#ifndef DVDATABASECONTROLLER_H
#define DVDATABASECONTROLLER_H

#include <QObject>
#include "dvDefinitions.h"

class dvDatabaseController : public QObject
{
    Q_OBJECT

private:
    void open();
    void close();
    bool m_open;

public:
    explicit dvDatabaseController(QObject *parent = 0);
    ~dvDatabaseController();

    bool isConnectionOpen();
    QSqlDatabase db();

    bool getMapsTable(QSqlQuery* query);
    QVariant getMapFileForMap(QVariant mapId);
    bool insertMap(QVariant name, QVariant file);
    bool updateMap(QVariant id, QVariant name, QVariant file);
    bool deleteMap(QVariant id);
    bool isExistMap(QVariant name);

    bool getRegionsForMap(int map, QSqlQuery* query);
    bool isExistRegion(QVariant mapId, QVariant regionName);
    bool isUsedPolyInMap(QVariant mapId, QVariant poly);
    bool insertRegion(QVariant mapId, QVariant name, QVariant area);
    bool updateRegion(QVariant id, QVariant mapId, QVariant name, QVariant area);
    bool deleteRegion(QVariant id);
    QVariant getRegionName(QVariant id);

    bool getSynonymsForRegion(QVariant regionId, QSqlQuery* query);
    bool getRegionForName(QVariant mapId, QVariant name, QSqlQuery* query);
    bool getRegionForSynonym(QVariant mapId, QVariant synonym, QSqlQuery* query);
    bool isExistSynonymForRegion(QVariant regionId, QVariant synonym);
    bool isExistSynonymForMap(QVariant mapId, QVariant synonym);
    bool insertSynonym(QVariant regionId, QVariant synonym);
    bool updateSynonym(QVariant synonymId, QVariant regionId, QVariant synonym);
    bool deleteSynonym(QVariant synonymId);

    bool getDataForMapIndicator(int map, int indicator, QSqlQuery* query);
    bool getDataForMapIndicator(int map, int indicator, QSqlQuery* query, QDate start, QDate end);
    bool getDataForRegionIndicator(int region, int indicator, QSqlQuery* query);
    bool getDataForRegionIndicator(int region, int indicator, QSqlQuery* query, QDate start, QDate end);
    bool deleteFromDataWhere(const QVariant& date,
                             const QVariant& region,
                             const QVariant& indicator);
    bool isExistDataForRegionIndicator(const QVariant& date,
                                       const QVariant& region,
                                       const QVariant& indicator);
    bool insertData(const QVariant& date,
                    const QVariant& region,
                    const QVariant& indicator,
                    const QVariant& value);
    bool updateData(const QVariant& dateOld,
                    const QVariant& dateNew,
                    const QVariant& regionOld,
                    const QVariant& regionNew,
                    const QVariant& indicator,
                    const QVariant& value);

    bool getIndicatorsTable(QSqlQuery* query);
    bool getIndicatorsForMap(int map, QSqlQuery* query);
    bool getIndicatorsForRegion(int region, QSqlQuery* query);
    bool findIndicator(const int id, QSqlQuery* query);
    bool insertIndicator(const QVariant& id, const QVariant& name,
                         const QVariant& unit, const QVariant& info);
    bool updateIndicator(const QVariant& id, const QVariant& name,
                         const QVariant& unit, const QVariant& info);
    bool deleteFromIndicatorsWhere(QVariant id);    

    bool isExistData(QSqlQuery* query, int field, const QVariant& data);
    int  getMaxIntValue(QSqlQuery* query, int field);
    
signals:
    
public slots:
    
};

#endif // DVDATABASECONTROLLER_H
